package com.settlement.system.service.impl;

import com.settlement.system.model.entity.SysBillDateLink;
import com.settlement.system.mapper.SysBillDateLinkMapper;
import com.settlement.system.service.SysBillDateLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysBillDateLinkQuery;
import com.settlement.system.model.form.SysBillDateLinkForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.SysBillDateLinkImportVo;

/**
 * <p>
 * 各个店铺记账日期对应标准账单表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-10
 */
@Service
public class SysBillDateLinkServiceImpl extends ServiceImpl<SysBillDateLinkMapper, SysBillDateLink> implements SysBillDateLinkService {

    @Override
    public Page<SysBillDateLink> getPage(SysBillDateLinkQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysBillDateLink> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysBillDateLink(SysBillDateLinkForm sysBillDateLinkForm) {
         SysBillDateLink sysBillDateLink = new SysBillDateLink();
         BeanUtil.copyProperties(sysBillDateLinkForm, sysBillDateLink);
         return this.saveOrUpdate(sysBillDateLink);
     }

     @Override
     public SysBillDateLinkForm getSysBillDateLinkFormData(Long id) {
         SysBillDateLink sysBillDateLink = this.getById(id);
         SysBillDateLinkForm sysBillDateLinkForm = new SysBillDateLinkForm();
         BeanUtil.copyProperties(sysBillDateLink, sysBillDateLinkForm);
         return sysBillDateLinkForm;
     }

     @Override
     public boolean deleteSysBillDateLink(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysBillDateLinkImportVo> voList)  {
        List<SysBillDateLink> sysBillDateLinkList = new ArrayList<>();
        for (SysBillDateLinkImportVo importVo : voList) {
            SysBillDateLink sysBillDateLink = new SysBillDateLink();
            BeanUtil.copyProperties(importVo, sysBillDateLink);
            sysBillDateLinkList.add(sysBillDateLink);
        }
        this.saveOrUpdateBatch(sysBillDateLinkList);
    }

    @Override
    public List<SysBillDateLinkImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysBillDateLink> sysBillDateLinkList = this.getBaseMapper().selectBatchIds(ids);
        List<SysBillDateLinkImportVo> voList = new ArrayList<>();
        for (SysBillDateLink sysBillDateLink : sysBillDateLinkList) {
            SysBillDateLinkImportVo vo = new SysBillDateLinkImportVo();
            BeanUtil.copyProperties(sysBillDateLink, vo);
            voList.add(vo);
        }
        return voList;
    }
}
